//
//  GameCharge.m
//  EasyToo
//
//  Created by linshengqin on 13-8-31.
//
//

#import "GameChargeView.h"
#import "GameChargeConfirmViewController.h"
#import <QuartzCore/QuartzCore.h>
#import "DeviceUtil.h"
#import "GameManager.h"
#import "GameChargeInfo.h"
#import "GameInfo.h"
#import "NetworkAlertView.h"
#define SCREEN_HEIGHT [DeviceUtil getScreenHeight]
@implementation GameChargeView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        datamodel = [GameModel shareData];
        [self setBackgroundColor:BackgroundColor];

    }
    return self;
}


// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    [self initScrollView];
    [self initView];
}

-(void)initScrollView
{
    _bgScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, [DeviceUtil getScreenWidth], [DeviceUtil getScreenHeight])];
    [self addSubview:_bgScrollView];
    _bgScrollView.contentSize = CGSizeMake(320, 700);
    _bgScrollView.showsVerticalScrollIndicator = YES;
    
}

//-(id)init{}

-(void) initView
{

    [GameManager shareManager].delegate = self;
    [self loadGameData];
    [[NetworkAlertView shareView] showNetView];
    UILabel *titleLabel1 = [[UILabel alloc] initWithFrame:CGRectMake(30, 20.0f/480.0f*SCREEN_HEIGHT, 250, 30)];
    [titleLabel1 setText:@"选择游戏和金额"];
    [titleLabel1 setBackgroundColor:[UIColor clearColor]];
    [_bgScrollView addSubview:titleLabel1];
    
    UIImage *image = [UIImage imageNamed:@"select_button"];
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image] ;
    imageView.frame = CGRectMake(20, 30.0f/480.0f*SCREEN_HEIGHT, 10, 10);
    [_bgScrollView addSubview:imageView];
    
    self.gameName = [[UITextField alloc] initWithFrame:CGRectMake(30, 60.0f/480.0f*SCREEN_HEIGHT, 250, 30)];
    [self.gameName setBorderStyle:UITextBorderStyleBezel];
    self.gameName.borderStyle = UITextBorderStyleRoundedRect;
    self.gameName.layer.borderWidth = 1 ;
    self.gameName.layer.cornerRadius = 10 ;
    [_bgScrollView addSubview:self.gameName];
    self.gameName.delegate = self;
    self.gameName.tag = 1;
    
    // 建立 UIToolbar
    UIToolbar *toolBar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 44)];
    UIBarButtonItem *right = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(cancelPicker)];
    // 把按鈕加進 UIToolbar
    toolBar.items = [NSArray arrayWithObject:right];
    self.gameName.inputAccessoryView = toolBar;

    selectGamePicker = [[UIPickerView alloc]initWithFrame:CGRectZero];
    selectGamePicker.showsSelectionIndicator = YES;
    selectGamePicker.frame = CGRectMake(0, 100.0f/480.0f*SCREEN_HEIGHT, 0, 0);
    selectGamePicker.delegate = self;
    selectGamePicker.dataSource = self;
    selectGamePicker.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
    self.gameName.inputView = selectGamePicker;
    selectGamePicker.tag = 100;
    
    self.chargePrice = [[UITextField alloc] initWithFrame:CGRectMake(30, 100.0f/480.0f*SCREEN_HEIGHT, 250, 30)];
    [self.chargePrice setBorderStyle:UITextBorderStyleBezel];
    self.chargePrice.borderStyle = UITextBorderStyleRoundedRect;
    self.chargePrice.layer.borderWidth = 1 ;
    self.chargePrice.layer.cornerRadius = 10 ;
    [_bgScrollView addSubview:self.chargePrice];
    self.chargePrice.delegate = self;
    self.chargePrice.tag = 2;
    self.chargePrice.inputAccessoryView = toolBar;
    
    selectPricePicker = [[UIPickerView alloc]initWithFrame:CGRectZero];
    selectPricePicker.showsSelectionIndicator = YES;
    selectPricePicker.frame = CGRectMake(0, 100.0f/480.0f*SCREEN_HEIGHT, 0, 0);
    selectPricePicker.delegate = self;
    selectPricePicker.dataSource = self;
    selectPricePicker.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
    self.chargePrice.inputView = selectPricePicker;
    selectPricePicker.tag = 200;
    
    
    UILabel *titleLabel2 = [[UILabel alloc] initWithFrame:CGRectMake(30, 150.0f/480.0f*SCREEN_HEIGHT, 250, 30)];
    [titleLabel2 setText:@"填写帐号信息"];
    [titleLabel2 setBackgroundColor:[UIColor clearColor]];
    [_bgScrollView addSubview:titleLabel2];
    
    UIImageView *imageView2 = [[UIImageView alloc] initWithImage:image] ;
    imageView2.frame = CGRectMake(20, 160.0f/480.0f*SCREEN_HEIGHT, 10, 10);
    [_bgScrollView addSubview:imageView2];
    
    self.account = [[UITextField alloc] initWithFrame:CGRectMake(30, 190.0f/480.0f*SCREEN_HEIGHT, 250, 30)];
    [self.account setBorderStyle:UITextBorderStyleBezel];
    self.account.borderStyle = UITextBorderStyleRoundedRect;
    self.account.layer.borderWidth = 1 ;
    self.account.layer.cornerRadius = 10 ;
    self.account.placeholder = @"充值帐户";
    [_bgScrollView addSubview:self.account];
    self.account.delegate = self;
    self.account.tag = 3;
    
    UILabel *titleLabel3 = [[UILabel alloc] initWithFrame:CGRectMake(30, 240.0f/480.0f*SCREEN_HEIGHT, 250, 30)];
    [titleLabel3 setText:@"选择支付方式"];
    [titleLabel3 setBackgroundColor:[UIColor clearColor]];
    [_bgScrollView addSubview:titleLabel3];
    
    UIImageView *imageView3 = [[UIImageView alloc] initWithImage:image] ;
    imageView3.frame = CGRectMake(20, 250.0f/480.0f*SCREEN_HEIGHT, 10, 10);
    [_bgScrollView addSubview:imageView3];
    
//    self.zfbButton = [[UIButton alloc] initWithFrame:CGRectMake(30, 280.0f/480.0f*SCREEN_HEIGHT, 100, 30)];
//    //[self.zfbButton setBackgroundImage:[UIImage imageNamed:@"zfb"] forState:UIControlStateNormal];
//    [self.zfbButton addTarget:self action:@selector(zfbButtonPick:) forControlEvents:UIControlEventTouchDown];
//    [self.zfbButton setBackgroundImage:[UIImage imageNamed:@"zfb.png"] forState:UIControlStateNormal];
//    [self.zfbButton setBackgroundImage:[UIImage imageNamed:@"zhifubao2.png"] forState:UIControlStateSelected];
//    [_bgScrollView addSubview:self.zfbButton];
    
    self.ylButton = [[UIButton alloc] initWithFrame:CGRectMake(100, 280.0f/480.0f*SCREEN_HEIGHT, 100, 30)];
    [self.ylButton addTarget:self action:@selector(ylButtonPick:) forControlEvents:UIControlEventTouchDown];
    [self.ylButton setBackgroundImage:[UIImage imageNamed:@"yinlian1.png"] forState:UIControlStateNormal];
    [self.ylButton setBackgroundImage:[UIImage imageNamed:@"yinlian2.png"] forState:UIControlStateSelected];
    [_bgScrollView addSubview:self.ylButton];
    
    UIButton *nextStep =[UIButton buttonWithType:UIButtonTypeCustom];
    nextStep.frame = CGRectMake(40, 330.0f/480.0f*SCREEN_HEIGHT, 230, 40);
    [nextStep setBackgroundImage:[UIImage imageNamed:@"button_3"] forState:UIControlStateNormal];
    [nextStep setTitle:@"下一步" forState:UIControlStateNormal];
    [_bgScrollView addSubview:nextStep];
    [nextStep addTarget:self action:@selector(confirmMessge:) forControlEvents:UIControlEventTouchUpInside];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    // When the user presses return, take focus away from the text field so that the keyboard is dismissed.
    NSTimeInterval animationDuration = 0.30f;
    [UIView beginAnimations:@"ResizeForKeyboard" context:nil];
    [UIView setAnimationDuration:animationDuration];
    CGRect rect = CGRectMake(0.0f, 0.0f, self.frame.size.width, self.frame.size.height);
    self.frame = rect;
    [UIView commitAnimations];
    [textField resignFirstResponder];
    return YES;
}

-(void)confirmMessge:(id)sender
{
    /*
     if( [self.account.text isEqualToString:self.account.text] == NO)
     {
     UIAlertView *av= [[UIAlertView alloc] initWithTitle:@"提示" message:@"请确认帐号" delegate:self  cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
     [av show];
     }
     else*/
    {
        GameChargeConfirmViewController  *confirmView = [[GameChargeConfirmViewController alloc] init];
        [self.fatherView.navigationController pushViewController:confirmView animated:YES];
    }
    
}


#pragma picker delegate
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    printf("%s,%d\n,pickview tag is:%d\n",__func__,__LINE__,pickerView.tag);
    return 1;
}
-(NSInteger) pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)componen
{
    if(pickerView.tag == 100)
    {
        printf("pickerarray count is:%d",[datamodel.gameArray count]);
        return [datamodel.gameArray count];
    }
    else if(pickerView.tag == 200)
    {
        printf("pickerarray count is:%d",[datamodel.priceArray count]);
        return [datamodel.priceArray count];
    }
    else return 0;
}
-(NSString*) pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{

    if(pickerView.tag == 100)
    {
        return [datamodel.gameArray objectAtIndex:row];
    }
    else if(pickerView.tag == 200)
    {
        return [datamodel.priceArray objectAtIndex:row];

    }
    else return nil;
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{

    UILabel* pickerLabel = (UILabel*)view;
    if (!pickerLabel){
        pickerLabel = [[UILabel alloc] init];
        // Setup label properties - frame, font, colors etc
        //adjustsFontSizeToFitWidth property to YES
        pickerLabel.adjustsFontSizeToFitWidth = YES;
        [pickerLabel setTextAlignment:UITextAlignmentCenter];
        [pickerLabel setBackgroundColor:[UIColor clearColor]];
        [pickerLabel setFont:[UIFont boldSystemFontOfSize:15]];
    }
    // Fill the label text here
    pickerLabel.text=[self pickerView:pickerView titleForRow:row forComponent:component];
    return pickerLabel;
}

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    CGRect frame = textField.frame;
    int offset = frame.origin.y + 32 - (self.frame.size.height - 216.0);//键盘高度216
    NSTimeInterval animationDuration = 0.30f;
    [UIView beginAnimations:@"ResizeForKeyBoard" context:nil];
    [UIView setAnimationDuration:animationDuration];
    float width = self.frame.size.width;
    float height = self.frame.size.height;
    if(offset > 0)
    {
        CGRect rect = CGRectMake(0.0f, -offset,width,height);
        self.frame = rect;
    }
    [UIView commitAnimations];
}

-(void)textFieldDidEndEditing:(UITextField *)textField{
    if( textField.tag == 1)
    {
        NSInteger row = [selectGamePicker selectedRowInComponent:0];
        printf("textField tag is:%d,row is%d\n",textField.tag,row);
        self.gameName.text = [datamodel.gameArray objectAtIndex:row];
        [datamodel.gameName setString:[datamodel.gameArray objectAtIndex:row]];
        [datamodel.gameId setString:[datamodel.gameIdArray objectAtIndex:row]];
        [self checkGameChargeInfo];

    }
    if( textField.tag == 2)
    {
        NSInteger row = [selectPricePicker selectedRowInComponent:0];
        printf("textField tag is:%d,row is%d\n",textField.tag,row);
        self.chargePrice.text = [datamodel.priceArray objectAtIndex:row];
        [datamodel.price setString:[datamodel.priceArray objectAtIndex:row]];
    }
    if( textField.tag == 3)
    {
        [datamodel.account setString:self.account.text];
    }

}

-(void) checkGameChargeInfo
{
    //[GameManager shareManager].delegate = self;
    
    [[GameManager shareManager] checkGameChargeInfo:datamodel.gameId gameName:datamodel.gameName];
}

- (void)onCheckGameChargeInfoFinish:(BOOL)isSuccess
                           gameInfo:(NSMutableArray*)gameChargeInfoArray
{
    if(isSuccess)
    {
        int i=0;
        [datamodel.priceArray removeAllObjects];
        for(GameChargeInfo *obj in gameChargeInfoArray)
        {
            NSLog(@"gameid is:%@,gamename is:%@,gameprice:%@\n",obj.gameId,obj.gameName,obj.gamePrice);
            [datamodel.gameIdArray addObject:(obj.gameId)];
            [datamodel.priceArray setObject:(obj.gamePrice) atIndexedSubscript:i++];
        }
        printf("pickerarray count is:%d\n",[datamodel.priceArray count]);
        NSLog(@"datamodel.pricdArray:%@\n", datamodel.priceArray);
        if([datamodel.priceArray count]>0)
            [selectPricePicker reloadAllComponents];
    }

    
}

// 按下完成鈕後的 method
-(void) cancelPicker {
    [self.gameName endEditing:YES];
    [self.chargePrice endEditing:YES];
}

//支付方式
-(void)zfbButtonPick:(id)sender
{
    [self.zfbButton setSelected:YES];
    [self.ylButton setSelected:NO];
    datamodel.payType = 0;
}



-(void)ylButtonPick:(id)sender
{
    [self.zfbButton setSelected:NO];
    [self.ylButton setSelected:YES];
    datamodel.payType = 1;
}

-(void) loadGameData
{
    [GameManager shareManager].delegate = self;
    [[GameManager shareManager] searchAllGame];
    //NSArray *array = [NSArray arrayWithObjects:@"1",@"5",@"10",@"15",@"30",@"50",@"100", nil];
    //[game_model.priceArray setArray:array];
    
}

- (void)onSearchAllGameFinish:(BOOL)isSuccess
                        Games:(NSMutableArray*)gameInfoArray
{
    if(isSuccess)
    {
        printf("%s,%d\n",__func__,__LINE__);
        for(GameInfo *obj in gameInfoArray)
        {
            NSLog(@"gameid is:%@,gamename is:%@\n",obj.gameId,obj.gameName);
            [datamodel.gameArray addObject:(obj.gameName)];
            [datamodel.gameIdArray addObject:(obj.gameId)];
        }
        NSLog(@"datamodel is:%@",datamodel.gameArray);
        printf("pickerarray count is:%d",[datamodel.gameArray count]);
        
        //   game_model.pickerArray = [[NSArray arrayWithArray:gameInfoArray] retain];
    }
    [[NetworkAlertView shareView] delView];

}


@end
